package com.hou.shop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hou.shop.product.entity.AttrEntity;
import com.hou.shop.product.service.AttrAttrgroupRelationService;
import com.hou.shop.product.service.AttrService;
import com.hou.shop.product.service.CategoryService;
import com.hou.shop.product.vo.AttrGroupWithAttrsVo;
import com.hou.shop.product.vo.AttrRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hou.shop.product.entity.AttrGroupEntity;
import com.hou.shop.product.service.AttrGroupService;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.R;


/**
 * 属性分组
 *
 * @author houzongdu
 * @email 1716532921@qq.com
 * @date 2022-01-16 16:38:38
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /*
     * 获取关联信息
     * */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrGroupId) {

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupId);

        return R.ok().put("data", attrEntities);
    }

    /*
    * 获取没有关联的信息
    * */
    // /product/attrgroup/{attrgroupId}/noattr/relation

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable Long attrgroupId,
                            @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.getNoAttrInfo(attrgroupId,params);

        return R.ok().put("page",page);
    }

    /*
    * 添加属性与分组关系
    * */
    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addAttrAndGroupRelation(@RequestBody AttrRelation[] attrRelations){
        attrAttrgroupRelationService.saveData(attrRelations);
        return R.ok();
    }

    /*
     * 删除关联关系
     * */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrRelation[] attrRelations) {
        attrAttrgroupRelationService.deleteRealtion(attrRelations);
        return R.ok();
    }

    /*
    * 获取分类下所有分组&关联属性
    * */
    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R Withattr (@PathVariable Long catelogId) {

        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageById(params, catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();

        // 查询出该id的完整路径
        Long[] path = categoryService.getPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
